package org.siam.impl.schemas {
	
	import org.siam.core.schemas.ISchema;
	import org.siam.core.schemas.ISchemaFactory;
	import org.siam.core.schemas.ISchemaProperty;
	import org.siam.utils.RuntimeError;
	
	public class SchemaFactory implements ISchemaFactory {
		
		private var _configuration:XML;
		
		public function SchemaFactory(configuration:XML) {
			_configuration = configuration;
		}
		
		public function newInstance():ISchema {
			var schema:Schema = new Schema();
			var schemaId:String = _configuration["@id"];
			schema._id = schemaId;
			for each (var propertyXML:XML in _configuration["property"]) {
				var property:ISchemaProperty = new SchemaPropertyFactory(propertyXML).newInstance();
				var propertyId:String = property.id;
				if (!propertyId)
					throw new RuntimeError("A property must have a valid id for schema '%s'", schemaId);
				else if (schema.hasProperty(propertyId)) {
					throw new RuntimeError("Duplicated property id '%s' for schema '%s'", propertyId, schemaId);
				}
				schema.addProperty(property);
			}
			return schema;
		}
	}
}